package uk.co.q3c.deplan.client.domain.resource;

import java.util.Date;
import java.util.LinkedList;

import uk.co.q3c.deplan.client.domain.util.DailyProfile_LinkedList;
import uk.co.q3c.deplan.client.util.Q3DateUtil;

import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Serialized;
import com.googlecode.objectify.annotation.Unindexed;

/**
 * Originally inten ded to use a TreeSet for this implementation but found a bug
 * http://code.google.com/p/googleappengine/issues/detail?id=2876 which prevent that
 * 
 * @author David Sowerby 26 Feb 2010
 * 
 */
@Unindexed
@Cached(expirationSeconds = 600)
public class ResourceDailyProfile_LinkedList extends DailyProfile_LinkedList<ResourceProfileEntry> {

	private static final long serialVersionUID = 1L;

	@Serialized
	private LinkedList<ResourceProfileEntry> profile = new LinkedList<ResourceProfileEntry>();

	/**
	 * @param dao
	 */
	public ResourceDailyProfile_LinkedList() {
		super();
	}

	@Override
	protected LinkedList<ResourceProfileEntry> profile() {
		return profile;
	}

	public ResourceProfileEntry getEntry(Date date, boolean createEntry) {
		ResourceProfileEntry rpe = super.getEntry(date);
		if (rpe == null) {
			rpe = new ResourceProfileEntry(Q3DateUtil.dateToKey(date));
			addEntry(rpe);
		}
		return rpe;
	}

	public int totalUtilisedTime() {
		int total = 0;
		for (ResourceProfileEntry rpe : profile) {
			total += rpe.getUtilisedTime();
		}
		return total;
	}

	public void removeEntry(ResourceProfileEntry rpe3) {
		profile.remove(rpe3);

	}

}
